﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:APP_PROMETINFO
	/// </summary>
	public partial class APP_PROMETINFO : BaseDAL<GSLMS.Entity.TB_APP_PROMETINFO>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public APP_PROMETINFO()
		{}
		#region  Method



        /// <summary>
        /// 提取材料收集信息列表
        /// </summary>
        /// <param name="proID">立项申请记录ID</param>
        /// <returns></returns>
        public List<GSLMS.Entity.TB_APP_PROMETINFO> GetAll(long proID)
        {
            List<GSLMS.Entity.TB_APP_PROMETINFO> metinfoList = new List<Entity.TB_APP_PROMETINFO>();

            OracleParameter[] parms = new OracleParameter[] { 
              new OracleParameter("P_PROID",proID),
              new OracleParameter("p_cur",OracleType.Cursor)
            };

            parms[0].Direction = ParameterDirection.Input;
            parms[1].Direction = ParameterDirection.Output;

            DataSet ds = OracleHelper.ExecuteDataset(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_ProMetinfo_get", parms);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    GSLMS.Entity.TB_APP_PROMETINFO model = new Entity.TB_APP_PROMETINFO();
                    model = DataRowToModel(dt.Rows[i]);
                    metinfoList.Add(model);
                }
            }
           


            return metinfoList;

        }

        /// <summary>
        /// 新增材料收集信息，批量新增
        /// </summary>
        /// <param name="metinfoList">材料收集信息</param>
        /// <returns></returns>
        public bool AddAll(List<GSLMS.Entity.TB_APP_PROMETINFO> metinfoList)
        {
            bool flag = true;
            if (metinfoList != null && metinfoList.Count > 0)
            {
                OracleConnection con = new OracleConnection(OracleHelper.connectionString);
                con.Open();
                OracleTransaction tran = con.BeginTransaction();
                OracleCommand cmd = con.CreateCommand();
                cmd.Transaction = tran;

                int num = 0;

                try
                {
                    foreach (var model in metinfoList)
                    {

                        if (num == 0)
                        {
                            OracleParameter[] parms = new OracleParameter[] { 
                                       new OracleParameter("P_PROID",model.PROID),
                                       new OracleParameter("returnvalue",OracleType.Int32)
                                };

                            parms[0].Direction = ParameterDirection.Input;
                            parms[1].Direction = ParameterDirection.Output;
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "pkg_apply.proc_ProMetinfo_delete";
                            cmd.Parameters.AddRange(parms);
                            cmd.ExecuteNonQuery();
                            if (parms[1].Value == DBNull.Value || OracleHelper.ToInt(parms[1].Value.ToString()) == 0)
                            {
                                tran.Rollback();
                                cmd.Dispose();
                                tran.Dispose();
                                con.Close();
                                con.Dispose();
                                flag = false;
                                break;
                            }
                        }

                        OracleParameter[] parms2 = new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_PROID", model.PROID),
								new OracleParameter("P_REGINFOID", model.REGINFOID),
								new OracleParameter("P_INFONAME", model.INFONAME),
								new OracleParameter("P_ORIGINAL", model.ORIGINAL),
								new OracleParameter("P_ORICOPY", model.ORICOPY),
								new OracleParameter("P_COPIES", model.COPIES),
								new OracleParameter("P_REMARK", model.REMARK),
                                new OracleParameter("P_METINFODETAILID",model.MetInfoDetailID),
                                new OracleParameter("P_SORTNO",model.SortNo)};

                        for (int i = 0; i < 10; i++)
                        {
                            parms2[i].Direction = ParameterDirection.Input;
                        }

                        cmd.Parameters.Clear();
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "pkg_apply.proc_ProMetinfo_add";
                        cmd.Parameters.AddRange(parms2);
                        int effRows = cmd.ExecuteNonQuery();
                        if (effRows == 0)
                        {
                            tran.Rollback();
                            cmd.Dispose();
                            tran.Dispose();
                            con.Close();
                            con.Dispose();
                            flag = false;
                            break;
                        }

                        num++;
                    }

                    if (num == metinfoList.Count)
                    {
                        tran.Commit();

                    }
                }
                catch (Exception ex)
                {
                    log.ExceptionHand(ex);
                    tran.Rollback();

                    cmd.Dispose();
                    tran.Dispose();
                    con.Close();
                    con.Dispose();
                }
                finally
                {
                    cmd.Dispose();
                    tran.Dispose();
                    con.Close();
                    con.Dispose();
                }



            }

            return flag;
        }


		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_APP_PROMETINFO model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_PROID", model.PROID),
								new OracleParameter("P_REGINFOID", model.REGINFOID),
								new OracleParameter("P_INFONAME", model.INFONAME),
								new OracleParameter("P_ORIGINAL", model.ORIGINAL),
								new OracleParameter("P_ORICOPY", model.ORICOPY),
								new OracleParameter("P_COPIES", model.COPIES),
								new OracleParameter("P_REMARK", model.REMARK),
                                new OracleParameter("P_METINFODETAILID",model.MetInfoDetailID),
                                new OracleParameter("P_SORTNO",model.SortNo)

            
                         };
  
			for (int i = 0; i < 10; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_ProMetinfo_add", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_APP_PROMETINFO model)
		{
			bool flag = false;
            //OracleParameter[] parms =new OracleParameter[]{
            //                    new OracleParameter("P_ID", model.ID),
            //                    new OracleParameter("P_PROID", model.PROID),
            //                    new OracleParameter("P_REGINFOID", model.REGINFOID),
            //                    new OracleParameter("P_INFONAME", model.INFONAME),
            //                    new OracleParameter("P_ORIGINAL", model.ORIGINAL),
            //                    new OracleParameter("P_ORICOPY", model.ORICOPY),
            //                    new OracleParameter("P_COPIES", model.COPIES),
            //                    new OracleParameter("P_REMARK", model.REMARK)};
  
            //for (int i = 0; i < 8; i++)
            //{
            //    parms[i].Direction = ParameterDirection.Input;
            //}

            //try
            //{
            //    OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_app_prometinfo_update", parms);
            //    flag = true;
            //}
            //catch (Exception ex)
            //{
            //    log.ExceptionHand(ex);
            //}
			return flag;
		}

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
            //OracleParameter[] parms =new OracleParameter[]{
            //                    new OracleParameter("P_ID", ID)};
  
            //    parms[0].Direction = ParameterDirection.Input;

            //try
            //{
            //    OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_apply.proc_app_prometinfo_delete", parms);
            //    flag = true;
            //}
            //catch (Exception ex)
            //{
            //    log.ExceptionHand(ex);
            //}
			return flag;
		}

		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_APP_PROMETINFO DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_APP_PROMETINFO model = new GSLMS.Entity.TB_APP_PROMETINFO();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["PROID"]!=null && dr["PROID"].ToString()!="")
				{
					model.PROID=OracleHelper.ToLong(dr["PROID"].ToString());
				}
				if(dr["REGINFOID"]!=null && dr["REGINFOID"].ToString()!="")
				{
					model.REGINFOID=OracleHelper.ToLong(dr["REGINFOID"].ToString());
				}
				if(dr["INFONAME"]!=null && dr["INFONAME"].ToString()!="")
				{
					model.INFONAME=dr["INFONAME"].ToString();
				}
				if(dr["ORIGINAL"]!=null && dr["ORIGINAL"].ToString()!="")
				{
                    model.ORIGINAL = OracleHelper.ToInt(dr["ORIGINAL"].ToString());
				}
				if(dr["ORICOPY"]!=null && dr["ORICOPY"].ToString()!="")
				{
                    model.ORICOPY = OracleHelper.ToInt(dr["ORICOPY"].ToString());
				}
				if(dr["COPIES"]!=null && dr["COPIES"].ToString()!="")
				{
                    model.COPIES = OracleHelper.ToInt(dr["COPIES"].ToString());
				}
				if(dr["REMARK"]!=null && dr["REMARK"].ToString()!="")
				{
					model.REMARK=dr["REMARK"].ToString();
				}

                if (dr["METINFODETAILID"] != null && dr["METINFODETAILID"].ToString() != "")
                {
                    model.MetInfoDetailID = OracleHelper.ToLong(dr["METINFODETAILID"].ToString());
                }

                if (dr["SORTNO"] != null && dr["SORTNO"].ToString() != "")
                {
                    model.SortNo = OracleHelper.ToInt(dr["SORTNO"].ToString());
                }

				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_APP_PROMETINFO GetByID(long ID)
		{
			GSLMS.Entity.TB_APP_PROMETINFO sModel = new GSLMS.Entity.TB_APP_PROMETINFO();
            string sql = "";
            sql = "select * from TB_APP_PROMETINFO where id=" + ID.ToString();
            DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_APP_PROMETINFO> GetList(GSLMS.Entity.TB_APP_PROMETINFO searchParam)
		{
			var list=new List<GSLMS.Entity.TB_APP_PROMETINFO>();
			DataTable dt = null;
			string tblName = "TB_APP_PROMETINFO";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
			string strFields="*";
			int totalCount = 0;
			int countFlag = 1;
			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);
			if (dt != null && dt.Rows.Count > 0)
			{
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_APP_PROMETINFO model = new GSLMS.Entity.TB_APP_PROMETINFO();
					model = DataRowToModel(dt.Rows[i]);
					list.Add(model);
				}
			}
			else
			{
				return null;
			}
			return list;
		}

		#endregion  Method
	}
}

